package com.colin.util.util;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestClass {

    private static final Logger logger = LoggerFactory.getLogger(TestClass.class);

    private static ChildThreadPool threadPool = new ChildThreadPool();

    public static void main(String[] args) {
        MDC.put("traceId", "1232323213213");
        test3();
        System.out.println("12321321321");
        try {
            Thread.sleep(50000L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            MDC.clear();
        }
    }

    public static void test2() {
        threadPool.putThread(new Runnable() {
            @Override
            public void run() {
                System.out.println("2222222");
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("222222结束");
            }
        });
    }

    public static void test3() {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        List<CustomThreadPool.ThreadResult<List<JSONObject>>> result = threadPool.dealAndGetAsync(list, 2, new CustomThreadPool.RunFunction<List<JSONObject>, Integer>() {
            @Override
            public CustomThreadPool.ThreadResult<List<JSONObject>> run(List<Integer> list) {
                logger.info(JSONObject.toJSONString(list) + "=====开始=======");
                CustomThreadPool.ThreadResult<List<JSONObject>> result = new CustomThreadPool.ThreadResult<>();
                JSONObject jsonObject;
                List<JSONObject> tmp = new ArrayList<>();
                for (Integer a : list) {
                    jsonObject = new JSONObject();
                    jsonObject.put(a + "-", a);
                    tmp.add(jsonObject);
                }
                result.setData(tmp);
                logger.info(JSONObject.toJSONString(list) + "=====结束=======");
                logger.info("结果：" + JSONObject.toJSONString(result));
                try {
                    Thread.sleep(3000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return result;
            }
        });
        List<JSONObject> listr = new ArrayList<>();
        for(CustomThreadPool.ThreadResult<List<JSONObject>> rtl: result){
            listr.addAll(rtl.getData());
        }
        logger.info("最终结果：" + JSONObject.toJSONString(result));
    }
}
